//给数组添加消除重复方法
Array.prototype.unique = function()  
{  
      var   a   =   {};   
      for(var   i=0;   i<this.length;   i++)  
      {  
          if(typeof   a[this[i]]   ==   "undefined")  
              a[this[i]]   =   1;  
      }  
      this.length   =   0;  
      for(var   i   in   a)  
          this[this.length]   =   i;  
      return   this;  
}  
Ext.ns('Ext.ux.form');
Ext.ux.form.LovComboBox = Ext.extend(Ext.ux.form.LovCombo, {
	//overwrite
	getCheckedValue:function(field) {
		field = field || this.valueField;
		var c;
		if(field != this.displayField){
			if(this.value)
				c=this.value.split(',');
			else
				c=[];
		}else{
			c=[];
		}
		var snapshot = this.store.snapshot || this.store.data;
		snapshot.each(function(r) {
			if(r.get(this.checkField)) {
				c.push(r.get(field));
			}
		}, this);
		return c.unique().join(this.separator);
	},
	//overwrite
	onSelect:function(record, index) {
        if(this.fireEvent('beforeselect', this, record, index) !== false){
        	if(record.get(this.checkField) == true){
        		var c;
        		if(this.value)
        			c=this.value.split(',');
        		else
        			c=[];
        		for(var i = 0;i<c.length;i++){
        			if(c[i] == record.get(this.valueField)){
        				c.splice(i,1);
        			}
        		}
        		this.value = c.join(this.separator);
        	}
			record.set(this.checkField, !record.get(this.checkField));
			if(this.store.isFiltered()) {
				this.doQuery(this.allQuery);
			}
			this.setValue_(this.getCheckedValue());
            this.fireEvent('select', this, record, index);
        }
	},
//	getSelections:function(){
	getValue:function(){
		this.value = this.value || '';
		var a = this.value.split(',');
		for(var i=0;i<a.length;i++){
			if(this.points){
			a[i] = "'"+a[i]+"'"
			}else{
			a[i]=a[i];
			}
		}
		return a.join(this.separator);
    },
    setKey:function(key){
    	this.store.removeAll();
    	this.store.commitChanges();
    	this.store.baseParams.key =key;
    	this.store.load();
    },
	getCheckedDisplay:function() {
		var re = new RegExp(this.separator, "g");
		return this.getCheckedValue(this.displayField).replace(re, this.separator + ' ');
	},
    initComponent:function() {
		this.baseParams=this.baseParams||{};
		this.baseParams.is_new_version_ = true; //添加标志位，解决与未封装的老版本兼容问题
		if(!this.url){
			 if(this.isCommonAdapter){
			 	this.baseParams.adapter_id_=this.adapter_id_;
			 	this.url='/customerDetailQueryController.table';
			 }else{
			 	if(typeof(plcdataid)!="undefined"){
			 		this.baseParams.plcdataid=plcdataid;
				 	this.baseParams.plcdatakey=this.key;
				 	this.url='/customerDetailQueryController.table';
			 	}else{
			 		this.url='/combobox.report?decorate=no';
			 	}
			 }
		}
		if(this.data){ //直接提供数据
			this.mode="local";
			this.store = new Ext.data.SimpleStore({
				fields: ['name', 'valtext'],
				data : this.data
			});
			this.pageSize = null;
			delete this.data; //清除当前对象中data属性，否则有冲突
		}else{ //提供表名列名或key
			Ext.apply(this.baseParams,{dropdownlist:true,datasourceName:this.dataSource,table:this.table,column:this.column,key:this.key});
			this.mode="remote";
			this.store = new Ext.data.Store({
				url:getRootPath()+this.url,
				//autoLoad: {params:{start:0,limit:this.pageSize}},
				baseParams:this.baseParams,
				reader: new Ext.data.JsonReader({   
	                    totalProperty: 'totalCount',    
	                    root: 'data'  
	                },
	                [
	                 	{name: 'name'},{name:'valtext'}
	                ]
	            )
			});
		}
		Ext.ux.form.LovComboBox.superclass.initComponent.apply(this, arguments);
		this.onLoad = this.onLoad.createSequence(function() {
			if(this.el) {
				var v = this.el.dom.value;
				this.el.dom.value = '';
				this.el.dom.value = v;
			}
			this.setValue_(this.value);
		});
    },
    setSelections:function(value,text) {
    	text=text||value;
    	this.clearValue(); //删除值
	 	this.value=value; //设置值
	 	this.setRawValue(text); //设置显示文本
    },
    width:200,
    triggerAction: "all",
    displayField: "name",
    valueField: "valtext",
    emptyText : "请选择",
    loadingText : '正在加载信息',
    typeAhead:true,
    typeAheadDelay:0,
    minChars:1,
    listeners:{keyup:function(o,e){
    	var code = e.getKey();
    	//按退格键删除到空白时,自动查询
    	if(o.getRawValue().trim()=='' && code==8){
    		o.store.baseParams.query = o.getRawValue();
        	o.store.reload();
    	}
    }}
});
Ext.reg('xlovcombo', Ext.ux.form.LovComboBox);